﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Project_V1;

namespace Project_V1
{
    public partial class FrmKeyPadNum : Form
    {
        public static string input = string.Empty;
        public static bool confirm = false;
        public FrmKeyPadNum()
        {
            InitializeComponent();
        }

        string VariableName = string.Empty;
        public bool IsZero(string value)
        {
            // 定义一个非常小的数作为epsilon
            double epsilon = 1e-10; // 或者使用Double.Epsilon
            return Math.Abs(Convert.ToDouble(value)) < epsilon;
        }

        //在参数设置界面FrmParamSet,使用有参数构造方法传值
        public FrmKeyPadNum(string Variable)
        {
            InitializeComponent();
            if (IsZero(Variable)) Variable = "";
            this.txt_Modify.Text = Variable;//读取现在的值显示在Label
            VariableName = Variable;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            confirm = false;
            this.Dispose();
            this.Close();
        }

        //点击确定按钮后,将TextBox内的值写入PLC,(需要修改实时读取和停止线程)
        private void btn_Sure_Click(object sender, EventArgs e)
        {
            if (txt_Modify.Text == "")
            {
                confirm = false;
                this.Dispose();
                this.Close();
            }
            else
            {
                input = txt_Modify.Text;
                int dotCount = input.Count(c => c == '.');
                if (dotCount >= 2 || input.IndexOf(".") == 0 || input.IndexOf(".") == input.Length - 1)
                {
                    MessageBox.Show("数字输入错误");
                    return;
                }
                confirm = true;
                this.Dispose();
                this.Close();
            }
        }

        //Enter
        private void txt_Modify_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_Sure_Click(null, null);
            }
        }

        private void FrmKeyPadNum_Load(object sender, EventArgs e)
        {

        }
        private void FrmKeyPadNum_Shown(object sender, EventArgs e)
        {
            txt_Modify.Text = input;
            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            txt_Modify.SelectionLength = 0;
            txt_Modify.Focus();
        }

 

        void btn_Click(object sender, EventArgs e)
        {
            Button B = (Button)sender;
            string buttonText = B.Text;
            //int selectionStart = txt_Modify.SelectionStart; // 保存当前焦点位置
 
            int selectionStart = 0;// txt_Modify.SelectionStart; // 保存当前焦点位置
            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            selectionStart = txt_Modify.SelectionStart; // 保存当前焦点位置
            txt_Modify.Text = txt_Modify.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
            txt_Modify.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
            txt_Modify.Focus(); // 重新设置焦点到文本框
        }
        private void button8_Click(object sender, EventArgs e)
        {
            txt_Modify.Text = string.Empty;
            txt_Modify.Focus();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (txt_Modify.Text.Length == 0)
            {
                return;
            }
            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            int selectionStart = txt_Modify.SelectionStart;
            txt_Modify.Text = txt_Modify.Text.Remove(selectionStart - 1, 1);
            txt_Modify.SelectionStart = selectionStart - 1;
            txt_Modify.Focus();
        }

        private void txt_Modify_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && !char.IsControl(e.KeyChar))
            {
                e.Handled = true; // 取消输入
            }
            // 确保只能输入一个小数点
            if (e.KeyChar == '.' && ((TextBox)sender).Text.Contains('.'))
            {
                e.Handled = true; // 取消输入
            }
        }
    }
}
